using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Xml;

namespace Common.Utilities
{
    /// <summary>
    /// 
    /// </summary>
    public class SqlDbTypeUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="val"></param>
        /// <param name="dbType"></param>
        /// <returns></returns>
        public static object GetValueFromString(string val, DbType dbType)
        {
            switch(dbType)
            {
                case DbType.AnsiString:
                    return val;
                case DbType.AnsiStringFixedLength:
                    return val;
                case DbType.Binary:
                    return Encoding.Default.GetBytes(val);
                case DbType.Boolean:
                    return bool.Parse(val);
                case DbType.Byte:
                    return Encoding.Default.GetBytes(val)[0];
                case DbType.Currency:
                    return decimal.Parse(val);
                case DbType.Date:
                    return DateTime.Parse(val).Date;
                case DbType.DateTime:
                    return DateTime.Parse(val);
                case DbType.Decimal:
                    return decimal.Parse(val);
                case DbType.Double:
                    return double.Parse(val);
                case DbType.Guid:
                    return new Guid(val);
                case DbType.Int16:
                    return Int16.Parse(val);
                case DbType.Int32:
                    return Int32.Parse(val);
                case DbType.Int64:
                    return Int64.Parse(val);
                case DbType.Object:
                    return val;
                case DbType.SByte:
                    return sbyte.Parse(val);
                case DbType.Single:
                    return Single.Parse(val);
                case DbType.String:
                    return val;
                case DbType.StringFixedLength:
                    return val;
                case DbType.Time:
                    return DateTime.Parse(val).TimeOfDay;
                case DbType.UInt16:
                    return UInt16.Parse(val);
                case DbType.UInt32:
                    return UInt32.Parse(val);
                case DbType.UInt64:
                    return UInt64.Parse(val);
                case DbType.VarNumeric:
                    return val;
                case DbType.Xml:
                    XmlDocument xdoc=new XmlDocument();
                    xdoc.LoadXml(val);
                    return xdoc;
                default:
                    return val;
            }
        } 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbTypeString"></param>
        /// <returns></returns>
        public static DbType GetDbType(string dbTypeString)
        {
            for(int i=0;i<=23;i++)
            {
                DbType dbtype = (DbType) i;
                if(dbtype.ToString()==dbTypeString)
                    return dbtype;
            }
            return DbType.String;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbType"></param>
        /// <returns></returns>
        public static Type ToSystemType(DbType dbType)
        {
            switch (dbType)
            {
                case DbType.AnsiString:
                    return typeof(string);
                case DbType.AnsiStringFixedLength:
                    return typeof(string);
                case DbType.Binary:
                    return typeof (byte[]);
                case DbType.Boolean:
                    return typeof (bool);
                case DbType.Byte:
                    return typeof(byte);
                case DbType.Currency:
                    return typeof (decimal);
                case DbType.Date:
                    return typeof (DateTime);
                case DbType.DateTime:
                    return typeof(DateTime);
                case DbType.Decimal:
                    return typeof(decimal);
                case DbType.Double:
                    return typeof(decimal);
                case DbType.Guid:
                    return typeof (Guid);
                case DbType.Int16:
                    return typeof (int);
                case DbType.Int32:
                    return typeof(int);
                case DbType.Int64:
                    return typeof(int);
                case DbType.Object:
                    return typeof(string);
                case DbType.SByte:
                    return typeof(int);
                case DbType.Single:
                    return typeof (float);
                case DbType.String:
                    return typeof(string);
                case DbType.StringFixedLength:
                    return typeof(string);
                case DbType.Time:
                    return typeof(DateTime);
                case DbType.UInt16:
                    return typeof(int);
                case DbType.UInt32:
                    return typeof(int);
                case DbType.UInt64:
                    return typeof(int);
                case DbType.VarNumeric:
                    return typeof(string);
                case DbType.Xml:
                    return typeof (XmlDocument);
                default:
                    return typeof(string);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DbType ToDbType(Type type)
        {
            if (type == typeof (DateTime))
                return DbType.DateTime;
            else if (type == typeof (string))
                return DbType.String;
            else if (type == typeof (int))
                return DbType.Int32;
            else if (type == typeof (bool))
                return DbType.Boolean;
            else if (type == typeof (decimal))
                return DbType.Decimal;
            else if (type == typeof (byte))
                return DbType.Byte;
            else if(type==typeof(byte[]))
                return DbType.Binary;
            else if(type==typeof(float))
                return DbType.Single;
            else if(type==typeof(Guid))
                return DbType.Guid;
            else
                return DbType.String;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static SqlDbType ToSqlDbType(Type type)
        {
            if (type == typeof(DateTime))
                return SqlDbType.DateTime;
            else if (type == typeof(string))
                return SqlDbType.VarChar;
            else if (type == typeof(int))
                return SqlDbType.Int;
            else if (type == typeof(bool))
                return SqlDbType.Bit;
            else if (type == typeof(decimal))
                return SqlDbType.Decimal;
            else if (type == typeof(byte))
                return SqlDbType.Int;
            else if (type == typeof(byte[]))
                return SqlDbType.Image;
            else if (type == typeof(float))
                return SqlDbType.Float;
            else if (type == typeof(Guid))
                return SqlDbType.UniqueIdentifier;
            else
                return SqlDbType.VarChar;
        }
    }
}
